class Text(object):

  def __init__(self, text, font, maxwidth, color=(255, 255, 255)):
    self.__surfaces = []
    self.__width = 0
    self.__font = font
    self.__maxwidth = maxwidth
    self.__color = color
    self.__height = 0

    self.__generateSurfaces(text)

  def draw(self, graphics, coord):
    tempcoord = list(coord)
    for s in self.__surfaces:
      graphics.drawSurface(s, tempcoord)
      tempcoord[1] += s.get_height()

  def append(self, text):
    if isinstance(text, Text):
      for s in text.getSurfaces():
        self.__surfaces.append(s)
    elif isinstance(text, str):
      self.__generateSurfaces(text)

  def getWidth(self):
    return self.__width

  def getHeight(self):
    return self.__height

  def getSurfaces(self):
    return self.__surfaces

  def __generateSurfaces(self, text):
    textarr = text.split('\n')
    for t in textarr:
      s = self.__font.render(t, True, self.__color)
      if s.get_width() > self.__maxwidth:
        maxletters = t.rfind(' ', 0, int(round(float(self.__maxwidth)/s.get_width()*len(t)))-1)
        nt = t[maxletters:].strip()
        textarr.insert(textarr.index(t)+1, nt)
        ot = t[0:maxletters].strip()
        s = self.__font.render(ot, True, self.__color)

      if(s.get_width() > self.__width):
        self.__width = s.get_width()
      self.__height += s.get_height()

      self.__surfaces.append(s)
